<template>
  <div class="app-container">
    <el-row :gutter="20" class="question-edit-contain">
      <el-col :span="24">
        <el-card>
          <el-form
            :model="form"
            ref="form"
            label-width="100px"
            v-loading="formLoading"
            :rules="rules"
          >
            <el-form-item label="模块名称：" prop="name" required>
              <el-input v-model="form.name"> </el-input>
            </el-form-item>
            <el-form-item label="场景类型">
              <el-select
                class="filter-item"
                v-model="form.sceneType"
                :clearable="true"
                placeholder=""
                filterable
              >
                <el-option
                  v-for="item in sceneTypeList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="顺序：" prop="sortNum" required>
              <el-input-number
                v-model="form.sortNum"
                :min="1"
                :max="8"
                label="顺序"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="图标：" prop="imgUrl" required>
              <el-upload
                ref="upfile"
                :multiple="false"
                :on-remove="handleRemove"
                :on-exceed="handleExceed"
                :file-list="fileList"
                action="#"
                accept=".jpg,.png,.jpeg"
                list-type="picture-card"
                :limit="1"
                :http-request="uploadFile"
              >
                <i class="el-icon-plus" />
              </el-upload>
            </el-form-item>
            <el-form-item label="是否显示：" prop="showAble">
              <el-switch v-model="form.showAble" active-color="#13ce66" inactive-color="#ff4949">
              </el-switch>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { BizFileUploadController, BizChallengeModuleController, DictionaryController } from '@/api';
export default {
  name: 'formEditBizChallengeModule',
  props: {
    rowData: Object
  },
  data() {
    return {
      form: {
        id: null,
        name: null,
        sortNum: 1,
        imgUrl: null,
        showAble: true,
        sceneType: null
      },
      formLoading: false,
      sceneTypeList: [],
      rules: {
        name: [{ required: true, message: '请输入模块名称' }],
        sortNum: [{ required: true, message: '请输入排序' }]
      },
      fileList: []
    };
  },
  created() {
    this.loadSceneTypeDropdownList();
  },
  methods: {
    handleExceed(file) {
      this.$message.warning('只能上传一个图标');
    },
    handleRemove(file) {
      this.form.imgUrl = null;
    },
    uploadFile(file) {
      let fd = {
        file: file.file
      };
      BizFileUploadController.uploadImage(this, fd)
        .then(res => {
          this.form.imgUrl = res.data.downloadUri + '/' + res.data.filename;
          console.log(res.data.filename);
          this.$message.success('附件上传成功');
        })
        .catch(e => {
          console.error('请求失败', e);
          this.$message.error('附件上传失败');
        });
    },
    onCancel(isSuccess = false) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess);
      }
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true;
          BizChallengeModuleController.edit(this, this.form)
            .then(re => {
              if (re.errorCode === 'NO-ERROR') {
                this.$message.success('操作成功');
                this.onCancel(true);
                this.$router.replace({ name: 'formsBizChallengeModule' });
              } else {
                this.$message.error('操作失败');
                console.error(re.errorMessage);
              }
            })
            .finally(() => {
              this.formLoading = false;
            });
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.form = {
        name: null,
        sortNum: 0,
        imgUrl: null,
        showAble: true
      };
      this.$refs['form'].resetFields();
    },
    loadSceneTypeDropdownList() {
      return new Promise((resolve, reject) => {
        let params = {
          dictCode: 'SceneType'
        };
        DictionaryController.dictGlobalDict(this, params)
          .then(res => {
            this.sceneTypeList = res.getList();
          })
          .catch(e => {
            reject(e);
          });
      });
    }
  },
  mounted() {
    if (this.rowData != null) {
      this.form.id = this.rowData.id;
      this.form.name = this.rowData.name;
      this.form.sortNum = this.rowData.sortNum;
      this.form.imgUrl = this.rowData.imgUrl;
      this.form.showAble = this.rowData.showAble;
      this.fileList = [{ name: '', url: this.rowData.fullFilePath }];
      if (this.rowData.sceneType) {
        this.form.sceneType = this.rowData.sceneType + '';
      } else {
        this.form.sceneType = null;
      }
    }
  }
};
</script>
<style type="scss" scoped></style>
